//
//  MenuViewController.m
//  Cooking
//
//  Created by dllo on 15/8/22.
//  Copyright (c) 2015年 ZP.Y. All rights reserved.
//

#import "MenuViewController.h"
#import "AppMacro.h"
#import "MenuCollectionViewCell.h"
#import "AsyncGetConnection.h"
#import "FoodModel.h"
#import "MenuDetailsViewController.h"
#import "MBProgressHUD.h"
#import "SearchViewController.h"
#import "BaiduMapViewController.h"

@interface MenuViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, MBProgressHUDDelegate>

@property (retain, nonatomic) NSMutableArray   *collectionArray;
@property (retain, nonatomic) UICollectionView *menue;
@property (retain, nonatomic) MBProgressHUD    *HUD;

@end

@implementation MenuViewController

- (void)dealloc {
    [_collectionArray release];
    [_menue release];
    [_HUD release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.collectionArray = [NSMutableArray array];
    }
    return self;
}

- (void)viewWillAppear:(BOOL)animated {
    self.navigationController.navigationBar.hidden = NO;
    self.navigationController.navigationBar.translucent = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.translucent = NO;
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:255 / 255.0 green:125 / 255.0 blue:67 / 255.0 alpha:1];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:18], NSForegroundColorAttributeName : [UIColor whiteColor]}];
    self.navigationItem.title = @"菜谱";
    
    UIButton *bbutton = [[[UIButton alloc] initWithFrame:CGRectMake(0, 0, 32, 32)] autorelease];
    
    [bbutton setBackgroundImage:[UIImage imageNamed:@"75.png"] forState:UIControlStateNormal];
    
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:bbutton];
    
    [bbutton addTarget:self action:@selector(searchAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
     self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"附近美食" style:UIBarButtonItemStylePlain target:self action:@selector(baiduMap:)];
    
    
    [self createMenueCollectionView];
    [self beginToConnect];
}

// 搜索button的点击事件
-(void)searchAction:(UIBarButtonItem *)sender
{
    SearchViewController *search = [[[SearchViewController alloc]init]autorelease];
    [self.navigationController pushViewController:search animated:YES];
    
}

-(void)baiduMap:(UIBarButtonItem *)sender
{
    BaiduMapViewController *map = [[[BaiduMapViewController alloc]init]autorelease];
    map.hidesBottomBarWhenPushed = YES;
    
    [self.navigationController pushViewController:map animated:YES];
    
}

#pragma mark -
#pragma mark 网络数据解析
- (void)beginToConnect {
    [AsyncGetConnection asyncGetConnectionWithURL:@"http://121.40.54.242:80/HandheldKitchen/api/found/tblBoutique!getTblBoutiqueTypeList.do?is_traditional=0&page=1&pageRecord=12&phonetype=1;" parameters:nil block:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSArray *array = [dic objectForKey:@"data"];
        [_collectionArray removeAllObjects];
        for (NSDictionary *value in array) {
            FoodModel *foodModel = [[FoodModel alloc] init];
            [foodModel setValuesForKeysWithDictionary:value];
            [_collectionArray addObject:foodModel];
            [foodModel release];
        }
        [_menue reloadData];
        [_HUD hide:YES];
    }];
}

- (void)createMenueCollectionView {
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(WIDTH / 3 - 7, HEIGHT / 4 - 42);
//    flowLayout.minimumInteritemSpacing = 20;
//    flowLayout.minimumLineSpacing = 10;
//    flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    self.menue = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64 - 49) collectionViewLayout:flowLayout];
    _menue.backgroundColor = [UIColor colorWithRed:250 / 255.0 green:242 / 255.0 blue:230 / 255.0 alpha:1];
    _menue.delegate = self;
    _menue.dataSource = self;
    _menue.pagingEnabled = YES;
    [_menue registerClass:[MenuCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
    [self.view addSubview:_menue];
    [flowLayout release];
    [_menue release];
    
    //加载等待
    self.HUD = [[MBProgressHUD alloc] init];
    _HUD = [[MBProgressHUD showHUDAddedTo:self.view animated:YES] retain];
    _HUD.delegate = self;
    _HUD.backgroundColor = [UIColor clearColor];
    _HUD.detailsLabelText = @"努力加载中...";
    _HUD.dimBackground = YES;
}

#pragma mark -
#pragma mark collectionView协议方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _collectionArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    MenuCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    FoodModel *foodModel = [_collectionArray objectAtIndex:indexPath.item];
    [cell sendValueWithModel:foodModel];
//    cell.backgroundColor = [UIColor redColor];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    MenuDetailsViewController *menuDetailsVC = [[MenuDetailsViewController alloc] init];
    menuDetailsVC.typeId = [[_collectionArray objectAtIndex:indexPath.item] valueForKey:@"iD"];
    [self.navigationController pushViewController:menuDetailsVC animated:YES];
    NSLog(@"%@",indexPath);
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
